package com.jg.common;

/**
 * @BelongsProject: smartapp
 * @BelongsPackage: com.jg.common
 * @Author: YangMaozhi
 * @CreateTime: 2024-11-21  00:23
 * @Description: 统一返回前端结果
 * @Version: 1.0
 */

public class Result<T> {
    private String code; // 返回码
    private String msg; // 返回中文信息
    private T data;

    /* 不带参数的成功返回 */
    public static Result success() {
        Result result = new Result<>();
        result.setCode(ResultCode.SUCCESS.code);
        result.setMsg(ResultCode.SUCCESS.msg);
        return result;
    }

    /* 带参数的成功返回 */
    public static <T> Result<T> success(T data) {
        Result result = new Result<>(data);
        result.setCode(ResultCode.SUCCESS.code);
        result.setMsg(ResultCode.SUCCESS.msg);
        return result;
    }

    /* 不带参数的失败返回 */
    public static Result error() {
        Result result = new Result<>();
        result.setCode(ResultCode.ERROR.code);
        result.setMsg(ResultCode.ERROR.msg);
        return result;
    }

    /* 带错误提示的的失败返回 */
    public static Result error(String code, String msg) {
        Result result = new Result<>();
        result.setCode(code);
        result.setMsg(msg);
        return result;
    }



    public Result() {
    }

    public Result(T data) {
        this.data = data;
    }

    /**
     * 获取
     * @return code
     */
    public String getCode() {
        return code;
    }

    /**
     * 设置
     * @param code
     */
    public void setCode(String code) {
        this.code = code;
    }

    /**
     * 获取
     * @return msg
     */
    public String getMsg() {
        return msg;
    }

    /**
     * 设置
     * @param msg
     */
    public void setMsg(String msg) {
        this.msg = msg;
    }

    /**
     * 获取
     * @return data
     */
    public T getData() {
        return data;
    }

    /**
     * 设置
     * @param data
     */
    public void setData(T data) {
        this.data = data;
    }

    public String toString() {
        return "Result{code = " + code + ", msg = " + msg + ", data = " + data + "}";
    }
}
